﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChatForum.Business.Contract;
using ChatForum.Business.Entity;
using ChatForum.Business.Logic;
using ChatForum.Messaging;

namespace ChatForum.Application.Service
{
    public class AccountService : IAccountService
    {
		private SmtpServerBase smtpServer;
		public AccountService(IRepository repository, SmtpServerBase smtpServer)
			: base(repository)
		{
			this.smtpServer = smtpServer;
		}

        public override void RegisterUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            AccountLogic.RegisterMe(user, _repository);
        }

        public override void Login(string userId, string password)
        {
            if (string.IsNullOrWhiteSpace(userId))
                throw new ArgumentNullException("userId");
            if (string.IsNullOrWhiteSpace(password))
                throw new ArgumentNullException("password");

            AccountLogic.LogIn(userId, password, _repository);
        }

        public override void ChangePassword(string userId, string oldPassword, string newPassword)
        {
            if (string.IsNullOrWhiteSpace(userId))
                throw new ArgumentNullException("userId");
            if (string.IsNullOrWhiteSpace(oldPassword))
                throw new ArgumentNullException("oldPassword");
            if (string.IsNullOrWhiteSpace(newPassword))
                throw new ArgumentNullException("newPassword");

            AccountLogic.ChangeMyPassword(userId, oldPassword, newPassword, _repository);
        }

        public override void BlockUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            AccountLogic.BlockUser(user, _repository);

			if (smtpServer != null)
			{
				string messageBody = string.Format("Hi {0}\n\nYour account got disabled. Please reply to this mail thread for the reason.\n\nThanks & Regards\nAdmin.", user.UserId);
				string subject = "Your account has been disabled";
				smtpServer.SendAsynchMessage(null, Enumerable.Repeat<string>(user.EmailId, 1), messageBody, subject, Enumerable.Repeat<string>(user.AlternateEmailId, 1), null); 
			}
        }

		public override void UnblockUser(User user)
		{
			if (user == null)
				throw new ArgumentNullException("user");
			AccountLogic.UnblockUser(user, _repository);

			if (smtpServer != null)
			{
				string messageBody = string.Format("Hi {0}\n\nYour account got enabled. Please enjoy the forum service.\n\nThanks & Regards\nAdmin.", user.UserId);
				string subject = "Your account has been enabled";
                smtpServer.SendAsynchMessage(null, Enumerable.Repeat<string>(user.EmailId, 1), messageBody, subject, Enumerable.Repeat<string>(user.AlternateEmailId, 1), null);
			}
		}

        public override User GetUser(string userId)
        {
            if (string.IsNullOrWhiteSpace(userId))
                throw new ArgumentNullException("userId");
            return AccountLogic.GetUser(userId, _repository);
        }

        public override void ModifyAccountDetails(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            AccountLogic.ModifyAccount(user, _repository);
        }

        public override void Logout(string userId)
        {
            if (string.IsNullOrWhiteSpace(userId))
                throw new ArgumentNullException("userId");
            AccountLogic.Logout(userId, _repository);
        }

		public override IEnumerable<User> GetAllUser()
		{
			return AccountLogic.GetAllUser(_repository);
		}

		public override void SendMessage(User user, string subject, string body)
		{
			if (smtpServer == null)
				throw new ArgumentException("SMTP Server not configured.");
			smtpServer.SendMessage(null, Enumerable.Repeat<string>(user.EmailId, 1), body, subject, Enumerable.Repeat<string>(user.AlternateEmailId, 1), null);
		}
	}
}
